package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CartController
 * @Author 波
 * @Date 2024/8/10 15:45
 */
@RestController
@Tag(name = "购物车接口")
public class CartController {
    @Autowired
    private CartService cartService;

    @RequiresLogin
    @Operation(summary = "添加购物车")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum) {
        cartService.addToCart(skuId, skuNum);
        return AjaxResult.success();
    }

    @RequiresLogin
    @Operation(summary = "查询购物车列表")
    @GetMapping("/cartList")
    public AjaxResult cartList() {
        List<CartInfo> cartInfoList = cartService.cartList();
        return AjaxResult.success(cartInfoList);
    }

    @RequiresLogin
    @Operation(summary = "删除商品信息")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return AjaxResult.success();
    }

    @RequiresLogin
    @Operation(summary = "修改商品的选中状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable("skuId") Long skuId, @PathVariable("isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return AjaxResult.success();
    }

    @RequiresLogin
    @Operation(summary = "全选商品")
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable("isChecked") Integer isChecked) {
        cartService.allCheckCart(isChecked);
        return AjaxResult.success();
    }

    @RequiresLogin
    @Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart() {
        cartService.clearCart();
        return AjaxResult.success();
    }


    @InnerAuth
    @GetMapping("/getCartCheckedList/{userId}")
    public R<List<CartInfo>> getCartCheckedList(@PathVariable("userId") Long userId) {
        List<CartInfo> cartInfoList = cartService.getCartCheckedList(userId);
        return R.ok(cartInfoList);
    }

    @InnerAuth
    @GetMapping("/updateCartCheckedPrice/{userId}")
    public R<Boolean> updateCartCheckedPrice(@PathVariable("userId") Long userId) {
        return R.ok( cartService.updateCartCheckedPrice(userId));
    }
    @InnerAuth
    @DeleteMapping("/deleteCartInfo/{userId}")
    public R<Boolean> deleteCheckedCartInfo(@PathVariable("userId") Long userId) {
        return R.ok( cartService.deleteCheckedCartInfo(userId));
    }

}
